var flag = false;  //存储是否还有更多数据
$(function () {
    $(".foot>a").on("touchstart", function () {
        if ($(this).hasClass("home")) {
            $(this).find("img").attr("src", "../img/homeActive.png");
            $(this).siblings("a").find("img").attr("src", "../img/my.png");
        } else {
            $(this).find("img").attr("src", "../img/myActive.png");
            $(this).siblings("a").find("img").attr("src", "../img/home.png");
        }
        $(this).find("p").css("color", "#FF3B43");
        $(this).siblings("a").find("p").css("color", "#A5A5A5");
    });

    $(".box ul").css("width", parseFloat($("body").width()) / 2 - 5 + "px");

    $(".black").height($(".foot").height());
    topage();

    $(window).scroll(function () {
        if ($(this).scrollTop() >= $("body").height() - $(this).height()) {
            if (!flag)
                topage();
        }
    });
});

//数据请求
function topage() {
    var page = $("#page").val();
    var token = $("#token").val();
    if (isempty(token))
        token = "null";
    var jsonstr = "{token:" + token + "}";
    var url = "/except/shopcontroller/shoplist.do";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            json: jsonstr,
            page:page
        },
        async: false,
        success: function (data) {
        	//console.log(data);
            var code = data.code;
            var message = data.message;
            if (code == 20013)
                flag = true;
            else if (code == 20000) {
                var usertype = data.data.usertype;
                var datas = data.data.shopinfo;
                $(datas).each(function () {
                    var clone = $(".clone").eq(0).clone(true);
                    clone.find("img").attr("src", this.img).attr("onclick", "location.href='/except/files/shop/shopinfo.jsp?shopid=" + this.shop_id + "'");
                    clone.find(".ills").html(this.shop_name);
                    clone.find(".price>span").html(this.price);
                    if(usertype * 1 == 0)
                        clone.find(".price>i").hide();
                    else if(usertype * 1 == 1)
                        clone.find(".price>i>b").html(this.first);
                    else if(usertype * 1 == 2)
                        clone.find(".price>i>b").html(this.second);
                    else if(usertype * 1 == 3)
                        clone.find(".price>i>b").html(this.third);
                    else if(usertype * 1 == 4)
                        clone.find(".price>i>b").html(this.forth);
                    else if(usertype * 1 == 5)
                        clone.find(".price>i>b").html(this.fifth);
                    else
                        clone.find(".price>i").hide();

                    if ($(".box .left").height() <= $(".box .right").height()) {
                        $(".box .left").append(clone);
                    } else {
                        $(".box .right").append(clone);
                    }
                    clone.fadeIn();
                });
                page++;
                $("#page").val(page);
                remove_advertising();
            }
        }
    });
}